<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="util.*"%>
<%@page import="dal.*"%>
<%@ page import="model.*,java.util.*"%>
<%
	String objectType=MiscUtil.getStringUrlParam(request, "objectType");
	int objectId=MiscUtil.getIntUrlParam(request, "objectId");
	
	String type="";
	String name="";
	if(objectType.equals("project"))
	{
		type="Project";
		name=CacheUtil.getProjectName(objectId);
	}
	if(objectType.equals("phase"))
	{
		type="Phase";
		name=CacheUtil.getPhaseName(objectId);
	}
	if(objectType.equals("checklist_item"))
	{
		type="Checklist Item";
		name=CacheUtil.getChecklistItemName(objectId);
	}
	if(objectType.equals(""))
	{
		type="Object";
		name="All Object";
	}
	
	GenericDal<DocumentModel, DocumentModelExt> dal = new GenericDal<DocumentModel, DocumentModelExt>(
			new DocumentModel());
	DocumentModelExt queryCount = new DocumentModelExt();
	if(!objectType.equals(""))
	{
		queryCount.setObjectType(objectType);
		queryCount.setObjectId(objectId);
	}
	int count = dal.count(queryCount);

	int pageIndex = MiscUtil.getIntUrlParam(request, "pageIndex", 1);

	int pageSize = MiscUtil.getIntUrlParam(request, "pageSize", 10);
	
	
	int noOfPage = count / pageSize;
	if (count % pageSize > 0) {
		noOfPage++;
	}

	DocumentModelExt query = new DocumentModelExt();
	query.setLimit(pageSize);
	query.setOffset((pageIndex - 1) * pageSize);
	if(!objectType.equals(""))
	{
		query.setObjectType(objectType);
		query.setObjectId(objectId);
	}
	List<DocumentModel> list = dal.selectList(query);
	boolean embedded = MiscUtil.getBoolUrlParam(request, "embedded", false);
%>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Document List</title>
	<%if(!embedded){ %>
		<link rel="stylesheet" href="css/listForm.css" />
		<link rel="stylesheet" href="css/common.css" />
		<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
		<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
		<link rel="stylesheet" href="css/jquery-dialog.css" />
		
		<script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
		<script language="javascript" type="text/javascript" src="js/common.js"></script>
	<%} %> 
	
	<script language="javascript" type="text/javascript">
		$(document).ready(pageLoaded);
		function pageLoaded()
		{	
			initDeleteConfirmation();
		} 
	</script>
</head>
<body>
	<%
		if (!embedded) {
	%>
	<div class="divContainer">
	<%@ include file="menu.jsp"%>
	<br />
	<b><%=type %>: <span><%=name%></span></b>
	<br />
	<br />
	<%if(!objectType.equals("")) {%>
	<a style="font-weight: bold;" href="documentNew.jsp?objectType=<%=objectType %>&objectId=<%=objectId %>">Attach New Document </a>
	<br />
	<%}%>
	<a style="font-weight: bold;" href="documentTypeList.jsp">Document Type List </a>
	<br />
	<br />
	<%}%>
	
	<div class="divListHeaderBar">NUMBER OF DOCUMENT : <%=count%></div>
	<div class="divOverFlow">
	<table class="tblList">
		<tr class="tblListHeader">
			<th class="thListHeader functionColumnNormal">Document</th> 
			
			<th class="thListHeader Id" >Id</th>
			<th class="thListHeader">Document Name</th>
			<th class="thListHeader">Document Type</th>
			<th class="thListHeader">Object Type</th>
			<th class="thListHeader">Object Id</th>
			<th class="thListHeader">Description</th>
			<th class="thListHeader">Remark</th>
			<th class="thListHeader"></th>
		</tr>
		

		<%
			int size = list.size();
			for (int i = 0; i < size; i++) {
				DocumentModel obj = list.get(i);
		%>

		<tr class="trListItem">
			<td class="tdListInt">
				<span><%=i + 1%></span>
				<br/>
				<span class="spanEdit"><a href="document?mode=edit&id=<%=obj.getId()%>">Edit</a></span>
				<br/>
				<span class="spanEdit"><a class="hrefDelete" href="document?mode=delete&id=<%=obj.getId()%>&objectType=<%=objectType %>&objectId=<%=objectId %>"  onclick="confirmToDelete();">Delete</a></span>
			</td>
			
			<td class="tdList"><%=obj.getIdToString()%></td>
			<td class="tdListText projectName"><%=obj.getDocumentNameToString()%></td>
			<td class="tdListInt"><%=CacheUtil.getDocumentTypeName(obj.getDocumentType())%></td>
			<td class="tdListText"><%=obj.getObjectTypeToString()%></td>
			<td class="tdListInt"><%=obj.getObjectIdToString()%></td>
			<td class="tdListTextarea"><%=obj.getDescriptionToString()%></td>
			<td class="tdListTextarea"><%=obj.getRemarkToString()%></td>
			<td class="tdListInt"><span class="spanEdit"><a href="">Download</a></span></td>
		</tr>
		<%
			}
		%>
	</table>
	</div>
	<br />
	<%
		if(noOfPage>1)
		{
	%>
	<table class="tblPaging">
		<tr class="trPaging">
			<td class="tdPaging">
				<%
					for (int i = 1; i <= noOfPage; i++) {
						if (i == pageIndex) {
				%> <span class="lblPaging"><%=i%></span> <%
 	} else {
 %> <a class="hrefPaging"
				href="documentList.jsp?pageIndex=<%=i%>&pageSize=<%=pageSize%>"><%=i%></a>
				<%
					}
					}
				%>
			</td>
		</tr>
	</table>
	<%} %>
	
	<%if(!embedded){ %> </div> <%} %>
</body>
</html>